<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 散列表子类
        class ValuePair {
            constructor(key, value) {
                this.key = key;
                this.value = value;
            }

            toString() {
                return `[${this.key}--${this.value}]`
            }
        }

        // 散列表实现
        class HashTable {
            constructor() {
                this.table = [];
            }


            // 对载入数据的预处理
            static loseloseHashCode(key) {
                let hash = 0;
                for (let i = 0; i < key.length; i++) {
                    hash += key.charCodeAt(i);
                }
                return hash % 37;
            }

            // 向散列表中插入值
            put(key, value) {
                let position = HashTable.loseloseHashCode(key);

                if (this.table[position] === undefined) {
                    this.table[position] = new ValuePair(key, value);
                } else {
                    let index = ++position;
                    while (this.table[index] !== undefined) {
                        index++;
                    }
                    this.table[index] = new ValuePair(key, value);
                }
            }

            // 获取元素
            get(key) {
                let position = HashTable.loseloseHashCode(key);

                if (this.table[position] !== undefined) {
                    if (this.table[position].key === key) {
                        return this.table[position].value;
                    } else {
                        let index = ++position;
                        while (this.table[index] === undefined || this.table[index].key !== key) {
                            // 判断是否大于离散表长度
                            if (index > this.table.length) {
                                return undefined;
                            }
                            index++;
                        }
                        if (this.table[index].key === key) {
                            return this.table[index].value;
                        }
                    }
                }
                return undefined
            }
            // 移除元素
            remove(key) {
                let position = HashTable.loseloseHashCode(key);

                if (this.table[position] !== undefined) {
                    if (this.table[position].key === key) {
                        this.table[position] = undefined;
                    } else {
                        let index = ++position;
                        while (this.table[index] === undefined || this.table[index].key !== key) {
                            // 判断是否大于离散表长度
                            if (index > this.table.length) {
                                return false;
                            }
                            index++;
                        }
                        if (this.table[index].key === key) {
                            this.table[index] = undefined;
                        }
                    }
                }
                return false;
            }
        }


        let table = new HashTable();
        table.put('haha', 10);
        table.put('pa', 20);
        table.put('rua', 30);
        // table.remove('rua');
        table.remove('rua');
        console.log(table);
        console.log(table.get('haha'));
    </script>
</body>

</html>